package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.List;

import org.jground.rx.IRXObject;
import org.jground.rx.RXAttribute;
import org.jground.rx.RXControl;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIStyles;
import org.jground.util.ArrayUtils;
import org.jground.util.CollectionUtils;
import org.jground.util.StringUtils;
import org.jground.zk.zulX.DivX;
import org.jground.zk.zulX.HboxX;
import org.jground.zk.zulX.VboxX;
import org.zkoss.zhtml.Div;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Html;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Popup;

public abstract class UIAttributeBase implements IUIAttribute<Component, UIFrame> {

    protected RXAttribute rxAttribute;
    protected RXView moView;
    //
    protected IUIAttributeView<Component, UIFrame> uiAttributeView;
    //
    protected IRXObject moDataObject;
    //
    private Div controlPanel;
    private VboxX labelComponent;

    private UIHistoryNavigation historyNavigation;
    private VboxX mandatoryMessageCont;

    protected UIAttributeBase() {}

    protected UIAttributeBase(UIFrame uiFrame, RXView moView) {

        this.moView = moView;
        this.rxAttribute = moView.getRXAttribute();

        controlPanel = new Div();
        
        // controlPanel.setWidth("99%");
        controlPanel.setStyle("width:100%;border: yellow solid 0px");
        // controlComponentDIV.setSclass(UIStyles.UI_CONTROL);
        mandatoryMessageCont = new VboxX();
        // LABEL
        //
        labelComponent = new VboxX();
        labelComponent.setStyle("padding: 2px;");
        //labelComponent.setStyle("width: 180px;");
        // labelComponent.setSclass(UIStyles.UI_LABEL);
        HboxX labelPart = new HboxX();
        // labelPart.setSclass(UIStyles.UI_LABEL);
        labelComponent.add(labelPart);
        //
        List<String> flags = new ArrayList<String>();
        String label = rxAttribute.getLabel();
        String description = rxAttribute.getDescription();

        if (rxAttribute.isMandatory()) {
            flags.add("M");
        }
        if (rxAttribute.isHistory() && moView instanceof RXControl) {
            // DivX historyPart = new DivX();
            historyNavigation = new UIHistoryNavigation(uiFrame, rxAttribute, this);
            // historyPart.add(historyNavigation);
            labelComponent.add(historyNavigation);
        }
        if (rxAttribute.isFourEyesApproval()) {
            flags.add("4Eyes");
        }
        if (CollectionUtils.isNotEmpty(flags)) {
            label += " [";
            label += ArrayUtils.join(flags.toArray(), " ");
            label += "]";
        }
        //
        // label creation
        //
        Label attributeLb = UIStyles.getInstance().getAttributeLabel(label + " ", rxAttribute.isMandatory());
        labelPart.add(attributeLb);
        if (StringUtils.isNotBlank(description)) {
            // attributeLb.setTooltiptext(description);
            Popup popup = new Popup();
            Html html = new Html(description);
            html.setParent(popup);
            attributeLb.setTooltip(popup);
            popup.setParent(labelComponent);

            Image helpImg = new Image();
            helpImg.setContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.info16));
            helpImg.setTooltip(popup);
            // helpImg.setStyle("cursor: help");
            labelPart.add(helpImg);
        }
        //
        labelComponent.add(mandatoryMessageCont);
    }

    public String getLabel() {
        return rxAttribute.getLabel();
    }

    public Component getLabelComponent() {
        return labelComponent;
    }

    public Component addToLabelComponent(Component component) {
        labelComponent.add(component);
        return component;
    }

    public RXAttribute getRXAttribute() {
        return this.rxAttribute;
    }

    public boolean checkMandatory() {
        if (rxAttribute.isMandatory() && uiAttributeView instanceof UIAttributeControlBase) {
            String[] sarray = StringUtils.removeEmptyElements(get());
            mandatoryMessageCont.removeAll();
            if (ArrayUtils.isEmpty(sarray)) {
                DivX div = null;
                this.mandatoryMessageCont.add(div = new DivX(new Label(getMandatoryCheckMessage())));
                div.setSclass(UIStyles.UI_MANDATORY_TEXT);
                return false;
            }
        }
        return true;
    }

    public void setData(IRXObject moDataObject) {
        this.moDataObject = moDataObject;
        if (uiAttributeView != null) {
            uiAttributeView.setDataContext(moDataObject);
        }
        if (historyNavigation != null) {
            historyNavigation.setMoDataObject(moDataObject);
        }
        String[] values = moDataObject.getValues(rxAttribute.getName());
        set(values);
    }

    public IRXObject updateData() {
        if (uiAttributeView instanceof UIAttributeControlBase) {
            this.moDataObject.set(rxAttribute.getName(), ((UIAttributeControlBase) uiAttributeView).get());
        }
        return this.moDataObject;
    }

    public void clearData() {
        moDataObject = null;
        if (uiAttributeView != null) {
            uiAttributeView.clear();
        }
    }

    public void set(String... values) {
        uiAttributeView.set(values);
    }

    public String[] get() {
        if (uiAttributeView instanceof UIAttributeControlBase) {
            return ((UIAttributeControlBase) uiAttributeView).get();
        }
        return null;
    }

    public Component getControlComponent() {
        return controlPanel;
    }

    public void addToControlComponent(Component... components) {
        for (Component component : components) {
            component.setParent(this.controlPanel);
        }
    }

    public String getMandatoryCheckMessage() {
        return "Mandatory!";
    }

    public RXView getRXView() {
        return this.moView;
    }

    public IUIAttributeView<Component, UIFrame> getUIView() {
        return uiAttributeView;
    }

}